import {dataRegionUtil} from "@/plugins/pageoffice/dataRegion.js";
import {scrollToTableBottom} from "@/utils/ruoyi.js";
import {onMounted, ref} from "vue";
import {isNull} from "@/utils/validate.js";

export function templateFunctions(addedDataRegionForm, addedDataRegions, addedDataRegionTableData, addedDataRegionTableRef,
                                  initDataRegions, rawInitDataRegions, initDataRegionForm, initDataRegionTableData, initDataRegionTableRef) {
    function initDataRegionData(rawDrData, addedDrData) {
        //过滤出剩余未被添加的预置数据区域
        let notAddedInitDataRegions = rawDrData.filter((item) => {
            let obj = addedDrData.find(obj => obj.name === item.name)
            return isNull(obj)
        })
        notAddedInitDataRegions = isNull(notAddedInitDataRegions) ? [] : notAddedInitDataRegions
        addedDataRegions.value = addedDrData
        addedDataRegionTableData.value = addedDrData
        initDataRegions.value = notAddedInitDataRegions
        initDataRegionTableData.value = notAddedInitDataRegions
        rawInitDataRegions.value = rawDrData
    }

    //加载预置的数据区域列表
    function queryInitDataRegion() {
        if (isNull(initDataRegionForm.value.initDataRegionNameForQuery)) {
            initDataRegionTableData.value = initDataRegions.value
        } else {
            initDataRegionTableData.value = initDataRegions.value.filter((data) => data.value.toLowerCase().indexOf(initDataRegionForm.value.initDataRegionNameForQuery.toLowerCase()) >=0)
        }
        nextTick(() => {
            scrollToTableBottom(initDataRegionTableRef)
        })
    }

    function addInitDataRegion(dataRegion) {
        let name = dataRegion.name
        //调用Pageoffice添加数据区域
        dataRegionUtil.addDataRegion(name, dataRegion.value)
        //更新预置表格数据
        initDataRegionTableData.value = initDataRegionTableData.value.filter(item => item.name !== name)
        initDataRegions.value = initDataRegions.value.filter(item => item.name !== name)
        //更新自定义表格数据
        addedDataRegions.value.push(dataRegion)
        addedDataRegionForm.value.dataRegionNameForQuery = ''
        queryDataRegion()
        nextTick(() => {
            scrollToTableBottom(addedDataRegionTableRef)
        })
    }

    //加载已添加的数据区域列表
    function queryDataRegion() {
        let dataRegionNameForQuery = addedDataRegionForm.value.dataRegionNameForQuery
        if (isNull(dataRegionNameForQuery)) {
            addedDataRegionTableData.value = addedDataRegions.value
        } else {
            addedDataRegionTableData.value = addedDataRegions.value.filter((data) => data.value.toLowerCase().indexOf(dataRegionNameForQuery.toLowerCase()) >=0)
        }
        nextTick(() => {
            scrollToTableBottom(addedDataRegionTableRef)
        })
    }

    function deleteInitDataRegion(dataRegion, index) {
        let drName = dataRegion.name
        //调用Pageoffice删除数据区域
        dataRegionUtil.deleteDataRegion(drName)
        //更新表格
        addedDataRegionTableData.value.splice(index, 1)
        addedDataRegions.value = addedDataRegions.value.filter((data) => data.name !== drName)
        //刷新预置表格
        initDataRegions.value.push(dataRegion)
        initDataRegionForm.value.dataRegionNameForQuery = ''
        queryInitDataRegion()
        nextTick(() => {
            scrollToTableBottom(initDataRegionTableRef)
        })
    }

    function resetDataRegionData() {
        //重置预置数据区域、自定义数据区域表格
        addedDataRegions.value = []
        addedDataRegionTableData.value = []
        initDataRegionTableData.value = rawInitDataRegions.value
    }

    return {
        initDataRegionData,
        queryInitDataRegion,
        addInitDataRegion,
        queryDataRegion,
        deleteInitDataRegion,
        resetDataRegionData
    }
}

export function templateFileLayout() {
    const titleRef1 = ref(null)
    const rowRef1 = ref(null)
    const titleRef2 = ref(null)
    const rowRef2 = ref(null)
    const tableHeight = ref(0)
    //除表格外其余多个元素之间的大概垂直间距总和，可根据实际情况自行调整
    const domVerticalMarginSum = 90

    onMounted(() => {
        // 在组件挂载时监听窗口大小变化
        window.addEventListener('resize', getTableAverageHeight)
        getTableAverageHeight()
    })

    // 在组件卸载时移除监听器
    onUnmounted(() => {
        window.removeEventListener('resize', getTableAverageHeight)
    })

    /**
     * 获取两个表格的平均高度
     */
    const getTableAverageHeight = () => {
        const windowHeight = window.screen.availHeight
        if (titleRef1.value &&
            rowRef1.value &&
            titleRef2.value &&
            rowRef2.value) {
            //固定元素高度总和
            const fixedDomHeight = (
                titleRef1.value.$el.offsetHeight +
                rowRef1.value.$el.offsetHeight +
                titleRef2.value.$el.offsetHeight +
                rowRef2.value.$el.offsetHeight
            )
            tableHeight.value = (windowHeight - fixedDomHeight - domVerticalMarginSum) / 2
        }
    }

    /**
     * 获取指定表格的高度
     * @param otherDomHeight 其他元素的高度
     * @returns {number}
     */
    const getSpecialTableHeight = (otherDomHeight) => {
        const windowHeight = window.screen.availHeight
        if (titleRef1.value &&
            rowRef1.value &&
            titleRef2.value &&
            rowRef2.value) {
            const domHeight = (
                titleRef1.value.$el.offsetHeight +
                rowRef1.value.$el.offsetHeight +
                titleRef2.value.$el.offsetHeight +
                rowRef2.value.$el.offsetHeight
            )
            return (windowHeight - domHeight - domVerticalMarginSum - otherDomHeight)
        }
    }

    return {
        titleRef1,
        rowRef1,
        titleRef2,
        rowRef2,
        tableHeight,
        getSpecialTableHeight
    }
}